package com.wg.dome.handle;

import com.wg.common.net.OperationRequest;
import com.wg.common.net.OperationResponse;
import com.wg.dome.ClientPeer;
import com.wg.dome.ErrorCode;
import com.wg.dome.ErrorHelper;
import com.wg.dome.annotation.Request;
import com.wg.dome.attribute.Attribute;
import com.wg.dome.attribute.AttributeHelper;
import com.wg.dome.entity.user.HeroEntity;
import com.wg.dome.operation.GetHeroInfoRequest;
import com.wg.dome.operation.GetHeroInfoResponse;
import com.wg.dome.operation.OperationCode;
import com.wg.dome.repository.HeroRepository;
import com.wg.common.net.SendParameters;

/**
 * 获取英雄信息
 */
@Request(OperationCode = OperationCode.GET_HERO_INFO)
public class HandleGetHeroInfoRequest extends BaseHandle {

    @Override
    public OperationResponse execute(ClientPeer peer, OperationRequest request, SendParameters sendParameters) {
        GetHeroInfoRequest getHeroInfoRequest = new GetHeroInfoRequest(request);

        if (!getHeroInfoRequest.isValid()) {
            return ErrorHelper.getInvalidResponse(getHeroInfoRequest);
        }
        if (getHeroInfoRequest.getPlayerId() != peer.getPlayerId()) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.PLAYERID_MATCH_ERROR, getHeroInfoRequest.getPlayerId(), peer.getPlayerId());
        }

        HeroRepository heroRepository = new HeroRepository();
        HeroEntity heroEntity = heroRepository.getEntity(getHeroInfoRequest.getPlayerId(), getHeroInfoRequest.getSzPos());
        if (heroEntity == null){
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.SZPOS_NULL);
        }

        Attribute fightAttribute = AttributeHelper.getSignHeroAttribute(peer.getPlayerId(), heroEntity.getSzPos());

        GetHeroInfoResponse getHeroInfoResponse = new GetHeroInfoResponse();
        getHeroInfoResponse.setSzPos(getHeroInfoRequest.getSzPos());
        getHeroInfoResponse.setSzAttribute(fightAttribute.toHashTable());
        return new OperationResponse(request.getOperationCode(), getHeroInfoResponse);
    }
}
